package com.bose.bosecmdclient;

import static com.bose.bosecmdclient.R.id.*;

import android.app.Activity;
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


public class AidlActivity extends Activity {
    CmdProxy mCmdProxy = null;
    public static final String INTENT_SERVICE_NAME_STR = "BOSE_SERVICE_NAME";

    @Override
    public void onStart() {
        super.onStart();
        String srvName = getIntent().getStringExtra(INTENT_SERVICE_NAME_STR);
        mCmdProxy = new CmdProxy(srvName);
        Log.d("BOSECMD_CLIENT", "onStart(), srvName=" + srvName);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aidl);
		
        AidlActivity.MyClickListener cl = new AidlActivity.MyClickListener();
        findViewById(button_aidl_send0).setOnClickListener(cl);
        findViewById(button_aidl_send1).setOnClickListener(cl);
        findViewById(button_aidl_send3).setOnClickListener(cl);
        findViewById(button_aidl_send_amsg).setOnClickListener(cl);
        findViewById(button_aidl_get_amsg).setOnClickListener(cl);
        findViewById(button_aidl_quit).setOnClickListener(view -> {
            finish();
        });
    }

    private class MyClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v){
            final int textId;
            if(v.getId() == button_aidl_send0)textId = text_aidl0;
            else if(v.getId() == button_aidl_send1)textId = text_aidl1;
            else if(v.getId() == button_aidl_send3)textId = text_aidl3;
            else if(v.getId() == button_aidl_get_amsg)textId = text_get_amsg;
            else if(v.getId() == button_aidl_send_amsg)textId = text_send_amsg;
            else return;

            if(null == mCmdProxy) return;

            String text = null;
            EditText et = findViewById(textId);
            if (null != et) {
                text = et.getText().toString();
            }
            final String cmd = text;
            String resp = "";
            int ret;
            if(button_aidl_send0 == v.getId()) {
                ret = mCmdProxy.CallSystem(cmd);
                resp = Integer.toString(ret);
            } else if(button_aidl_send1 == v.getId()) {
                ret = mCmdProxy.CallPopen(cmd, resp);
            } else if(button_aidl_get_amsg == v.getId()) {
                ret = mCmdProxy.GetAudioMessage(cmd, resp);
            } else if (button_aidl_send_amsg == v.getId()) {
                ret = mCmdProxy.SendAudioMessage(cmd);
                resp = Integer.toString(ret);
            }

            ((EditText)findViewById(text_aidl_response)).setText(resp);
        }
    }
}